package com.demo.util;

public class Base62Utils {
	
	/**
	 * 将数字转为62进制（不足6位的前面补0）
	 * @param num    Long 型数字
	 * @return 62进制字符串
	 */
	public static String base62Encode(long num) {
	    String chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
	    StringBuilder sb = new StringBuilder();
	    int remainder = 0;
	    int scale = 62;
	    while (num > scale - 1) { 
	        remainder = Long.valueOf(num % scale).intValue();
	        sb.append(chars.charAt(remainder));
	        num = num / scale;
	    }
	    sb.append(chars.charAt(Long.valueOf(num).intValue()));
	    String value = sb.reverse().toString();
	    StringBuilder result = new StringBuilder() ;
	    for(int i=0; i<6-value.length(); i++) {
	    	result.append("0") ;
	    }
	    return result.append(value).toString() ;
	}
	
	/**
	 * 62进制字符串转为数字
	 * @param str 编码后的62进制字符串
	 * @return 解码后的 10 进制字符串
	 */
	public static long base62Decode(String str) {
	    String chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
	    int scale = 62;
	    str = str.replace("^0*", "");
	    long num = 0;
	    int index = 0;
	    for (int i = 0; i < str.length(); i++) { 
	        index = chars.indexOf(str.charAt(i)); 
	        num += (long) (index * (Math.pow(scale, str.length() - i - 1)));
	    }
	    return num;
	}

}
